<!DOCTYPE html>
<html>
<head>
    <title>role form</title>
    <#include "/header.html">
    <link rel="stylesheet" href="${request.contextPath}/statics/js/lay-module/dtree/dtree.css" media="all">
    <link rel="stylesheet" href="${request.contextPath}/statics/js/lay-module/dtree/font/dtreefont.css" media="all">
</head>
<body class="bg-white">
<div id="appMain">
    <form class="layui-form layuimini-form" action="" lay-filter="dataForm">
        <div class="layui-form-item">
            <label class="layui-form-label required">角色</label>
            <div class="layui-input-block">
                <input type="text" name="roleName" lay-verify="required" lay-reqtext="角色名称不能为空" placeholder="请输入角色名称" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">授权</label>
            <div class="layui-input-block">
                <div id="menuListBox" class="demo-tree-more"></div>
            </div>
        </div>

        <div class="layui-form-item layui-hide">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" id="commitBtn" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript">
    layui.use(['form', 'dtree'], function () {
        var form = layui.form,
            dtree = layui.dtree,
            $ = layui.$;

        var _parent = parent;
        dtree.render({
            elem: "#menuListBox",
            data: _parent.menuTreeData,
            icon: "-1",
            skin: "laySimple",
            checkbar: true,
            checkbarType: "no-all", // 默认就是all，其他的值为： no-all  p-casc   self  only
            checkbarData: "halfChoose",
            done: function(res, $ul, first){
                if(first && _parent.formDataId > 0) {
                    $.ajax({
                        type: "POST",
                        url: serverUrl('sys/role/info/' + _parent.formDataId),
                        success: function (result) {
                            if (result.code === 0) {
                                form.val('dataForm', {
                                    "roleName": result.role.roleName,
                                    "remark": result.role.remark
                                });
                                if (result.role.menuIdList.length > 0) {
                                    dtree.chooseDataInit("menuListBox", result.role.menuIdList);
                                    dtree.initNoAllCheck("menuListBox");
                                }
                            }
                        }
                    });
                }
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var menuIdList = [];
            var dtreeNodes = dtree.getCheckbarNodesParam("menuListBox");
            $(dtreeNodes).each(function(i, row){
                menuIdList.push(row['nodeId'])
            });
            var params = {
                "roleId": parent.formDataId || null,
                "roleName": data.field.roleName,
                "remark": data.field.remark,
                "menuIdList": menuIdList
            };

            var method = parent.formDataId > 0 ? 'update' : 'save';
            submitFormData('sys/role/'+method, params);

            return false;
        });

    });
</script>
</body>
</html>